// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import HardModeGame from "./HardModeGame";

const {ccclass, property} = cc._decorator;

@ccclass
export default class EnemyBullet extends cc.Component {

    @property
    step: number = 0;  // 控制子弹移动速度

    canvas: cc.Node = null;
    canvasHeight: number = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.canvas = cc.find('Canvas');
        this.canvasHeight = this.canvas.height;
    }

    start () {

    }

    dismiss() {

        // 销毁子弹
        HardModeGame.inst.destroyEnemyBullet(this.node);
    }

    update (dt) {

        this.node.y -= this.step;

        let minY = -(this.canvasHeight / 2);

        // 超出屏幕范围
        if (this.node.y <= minY) {

            this.dismiss();

            cc.log('EnemyBullet超出视野, 自动销毁');
        }
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider) {

        this.dismiss();
    }
}
